Skip to content

gh-74667: Make socket.getservbyname(), getservbyport() and getprotobyname() thread-safe#151850

Open
tonghuaroot wants to merge 2 commits into
python:mainfrom
tonghuaroot:fix-socket-getservbyname-threadsafe
Open

gh-74667: Make socket.getservbyname(), getservbyport() and getprotobyname() thread-safe#151850
tonghuaroot wants to merge 2 commits into
python:mainfrom
tonghuaroot:fix-socket-getservbyname-threadsafe

Conversation

@tonghuaroot

@tonghuaroot tonghuaroot commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

socket.getservbyname(), getservbyport() and getprotobyname() release the GIL around the non-reentrant POSIX getservbyname(3) / getservbyport(3) / getprotobyname(3), which return a pointer into a shared static buffer, so concurrent calls (including on free-threading builds) can clobber each other's result.

This uses the reentrant *_r() variants where configure detects them, and otherwise serializes the lookups with the existing netdb_lock, mirroring how gethostbyname() is already handled in the same module. A regression test exercises concurrent lookups.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant